package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Category;
import org.vectrics.SystemException;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobFunction;
import org.vectrics.struts.VectricsTagSupport;

public class JobFunctionComboTag extends VectricsTagSupport {

	private static final long serialVersionUID = -6697760062117740880L;

	static private Category log = Category.getInstance(JobFunctionComboTag.class);
    private String styleClass = null;
    private String any = "false";
    private String none = "false";
    private String property = null;
    private String value = null;
    
    
    public JobFunctionComboTag() {
    	super();
    }

	public String makeStartTag() {
		StringBuffer html = new StringBuffer();
		
		log.debug("doStartTag() called");
		HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		String propValue = request.getParameter(property);
		log.debug("propValue = " + propValue);
		
		//HttpSession session = super.pageContext.getSession();
		if (styleClass == null) {
			html.append("<select name=\"" + this.property + "\" style=\"width:350;font-family:verdana,arial,sans serif;font-size=11px\">");
		} else {
			html.append("<select name=\"" + this.property + "\" class=\"" + styleClass + "\">");
		}
		
		boolean showAnyOption = false;
		try {
			if ((any != null) && (any.startsWith("tr"))) {
			    showAnyOption = true;
			}
		} catch (Exception ex) {
		    throw new SystemException("Exception converting to boolean: " + any);
		}
		
		if (showAnyOption)
		    html.append("<option value=\"\">" + this.getPhrase("phrase.any") + "</option>");
		
		if (none.toLowerCase().startsWith("t")) {
			html.append("<option value=\"\"></option>");
		}
		
		Iterator iter = RecruitServiceLocator.getJobProvider().findAllJobFunctions().iterator();
		while (iter.hasNext()) {
			JobFunction function = (JobFunction)iter.next();
			String selectStr = "";
			if (function.getCode().equals(this.value)) {
				selectStr = "SELECTED";
			}
			html.append("<option " + selectStr + " value=\"" + function.getCode() + "\">" 
					+ function.getDescription()	+ "</option>");
		}
		html.append("</select>");

		return(html.toString());
	}

	/**
	 * @return
	 */
	public String getStyleClass() {
		return styleClass;
	}

	/**
	 * @param styleClass
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * @return Returns the any.
	 */
	public String getAny() {
		return any;
	}
	
	/**
	 * @param any The any to set.
	 */
	public void setAny(String any) {
		this.any = any;
	}
	
	/**
	 * @return
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 */
	public void setProperty(String property) {
		this.property = property;
	}

	/**
	 * @return Returns the value.
	 */
	public String getValue() {
		return value;
	}
	/**
	 * @param value The value to set.
	 */
	public void setValue(String value) {
		this.value = value;
	}
	
	/**
	 * @return Returns the none.
	 */
	public String getNone() {
		return none;
	}
	/**
	 * @param none The none to set.
	 */
	public void setNone(String none) {
		this.none = none;
	}
	
}
